package com.hand.wfive.dialog

import android.content.Context
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.hand.wfive.R
import com.hand.wfive.adapter.PopupListAdapter
import com.hand.wfive.base.BasePopWindow
import com.hand.wfive.databinding.LayoutListPopupWindowBinding
import com.hand.wfive.entity.CommonModel
import com.hand.wfive.entity.ListItemDecoration
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener

/**
 * 列表popup window(白window单文字item)
 * 有时间再支持item布局 window背景自定义等
 */
class CommonListPopupWindow(context: Context, private val listData: List<CommonModel> ) : BasePopWindow<LayoutListPopupWindowBinding>(context) {
    private var mListener: ItemClickListener? = null
    private var adapter: PopupListAdapter? = null

    init {
        initData()
    }

    override fun initView() {
        super.initView()
    }

    private fun initData() {
        adapter = PopupListAdapter()
        mBinding.recyclerView.layoutManager = LinearLayoutManager(context)
        mBinding.recyclerView.addItemDecoration(ListItemDecoration(context))
        mBinding.recyclerView.adapter = adapter
        adapter?.setOnItemClickListener(OnItemClickListener { adapter1: BaseQuickAdapter<*, *>, view: View, position: Int ->
            mListener?.onItemClick(this@CommonListPopupWindow, adapter1, view, position)
            dismiss()
        })
        adapter?.setList(listData)
    }

    override fun getDarkViewId(): Int {
        return mBinding.viewDark.id
    }
    /**
     * 刷新选中的item
     */
    fun refreshSelectedItem(selectedId: Int) {
        if (listData == null || listData.isEmpty()) {
            return
        }
        for (item in listData) {
            item.isSelect = item.id == selectedId
        }
        adapter?.setList(listData)
    }

    fun setViewWidth(width: Int): CommonListPopupWindow {
        mBinding.layRoot.layoutParams = LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.WRAP_CONTENT)
        return this
    }

    fun setPopupWindowBackground(bgRes: Int): CommonListPopupWindow {
        if (bgRes > 0) {
            mBinding.layRoot.setBackgroundResource(bgRes)
        }
        return this
    }

    override fun initAnimStyle(context: Context) {
        super.initAnimStyle(context)
        setAnimStyleDown(context, mBinding.layRoot)
    }

    override fun showAsDropDown(anchor: View) {
        setAnimStyleDown(context, mBinding.layRoot)
        super.showAsDropDown(anchor)
    }

    override fun showAsDropDown(anchor: View, xoff: Int, yoff: Int) {
        setAnimStyleDown(context, mBinding.layRoot)
        super.showAsDropDown(anchor, xoff, yoff)
    }

    fun setItemClickListener(mListener: ItemClickListener?) {
        this.mListener = mListener
    }

    interface ItemClickListener {
        fun onItemClick(popupWindow: CommonListPopupWindow?, adapter: BaseQuickAdapter<*, *>, view: View, position: Int)
    }

    override fun getLayoutId(): Int {
        return R.layout.layout_list_popup_window
    }
}